package com.bear.springmvc.action.android;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bear.balance.BalanceService;
import com.bear.forum.UserService;
import com.bear.model.entity.User;

@Controller
public class AndroidAction {
	
	@Autowired
	private BalanceService balanceService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("/android/login.html")
	public void Login(HttpServletRequest request,
			   HttpServletResponse response)
	{
		String user = request.getParameter("user");
		String pass = request.getParameter("pass");
		boolean isValidUser = 
				userService.hasMathchUser(user, pass);
		int userId=-1;
		if(isValidUser)
		{
			User u = userService.findUserByUserName(user);
			userId = u.getId();
			request.getSession(true).setAttribute("userId", userId);
		}
		PrintWriter pw = null;
		try
		{
			JSONObject jsonObj = new JSONObject().put("userId", userId);
			response.setContentType("text/html;charset=UTF-8");
			pw = response.getWriter();
			pw.write(jsonObj.toString());
		}catch (Exception e) {
			pw.flush();
			pw.close();
		}finally{
			pw.flush();
			pw.close();
		}
	}

}
